/**
 * Created by user on 2015/7/14.
 */


angular.module('backofficeApp.numberGroup.controllers', []).
    controller('numberGroupCtrl', ['$scope', '$http', '$location', function ($scope, $http, $location) {


        $scope.groupId = null;

        //$scope.groupList = null;
        $scope.userList = [];

        $scope.getNumberGroupList = function () {

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/getNumberGroup', {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (results) {
                //console.debug(results.data);
                $scope.groupList = results.data;
            }).error(function () {

            });

        };
        $scope.getNumberGroupList();

        $scope.addGroup = function () {
            userNumberModel(true);
            $scope.userList = [];
            $scope.groupName="";
            $scope.sendNumber="";
            $scope.userRemark="";
            $scope.groupId = null;
        };


        $scope.editGroup = function (group) {

            $scope.userList = [];
            $scope.groupId =  group.objId;

            var number = "";
            group.sendNumbers.forEach(function(item){
                number += item.user.huanxinUserName +','
            });

            var numbers = number.split(",");
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/getUserInfo', numbers,{
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (results) {
                //$scope.userList = results.data;
                //if(results.data.length == 0){
                //    alert('发贴号码不存在!');
                //    return;
                //}
                results.data.forEach(function(res){

                    group.sendNumbers.forEach(function(item){
                        if(item.user.objId == res.objId){
                            res.userremark = item.userRemark;
                        }
                    });

                    $scope.userList.push(res);
                    $scope.groupName = group.groupName;
                    userNumberModel(true);
                });
            }).error(function () {
                alert('编辑号码组错误!');
            });
        };

        function userNumberModel(isShow) {
            if (isShow) {
                $('#usernumber_model').modal('show');
            }
            else {
                $('#usernumber_model').modal('hide');
            }
        }

        $scope.getNumberList = function () {

            var numbers = data.split(",");
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/getUserInfo', numbers,{
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (results) {
                $scope.userList = results.data;
                //console.debug(results);
                userNumberModel(true);
            }).error(function () {
            });
        };

        $scope.addNewNumber = function(number,remark){

            if (number == null || number == null || number == '') {
                alert('请输入发帖号!');
                return;
            };

            if( $scope.userList != null && $scope.userList.length>0) {
                for (var i = 0; i < $scope.userList.length; i++) {
                    if ($scope.userList[i].huanxinUserName == number) {
                        alert('该发贴号码已存在!');
                        $scope.sendNumber = "";
                        return;
                    }
                }
            };

            var numbers = number.split(",");
            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/getUserInfo', numbers,{
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (results) {
                //$scope.userList = results.data;
                if(results.data.length == 0){
                    alert('发贴号码不存在!');
                    return;
                }
                results.data.forEach(function(res){
                    res.userremark = remark;
                    $scope.userList.push(res);

                });
                $scope.userList.reverse();

            }).error(function () {
                alert('添加发贴号码失败!');
            });

            $scope.sendNumber="";
            $scope.userRemark="";
        }

        $scope.deleteNumber = function(data){

            $scope.userList.forEach(function(user){
                if(user.huanxinUserName == data.huanxinUserName){
                    $scope.userList.remove(user);
                    return;
                }
            });

        }

        $scope.saveGroup = function(){

            if ($scope.groupName == null || $scope.groupName == '') {
                alert('请输入名称!');
                return;
            };

            if ($scope.userList == null || $scope.userList == [] || $scope.userList == '') {
                alert('请添加发帖号!');
                return;
            };
            //console.debug($scope.userList);

            if($scope.groupId == null) {

                $scope.userList.groupName = $scope.groupName;

                $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/addNumberGroup/' + $scope.groupName, $scope.userList, {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).success(function (results) {
                    $scope.userList = null;
                    $('#usernumber_model').modal('hide');
                    $scope.getNumberGroupList();
                }).error(function () {
                    alert('添加号码组失败!');
                });
            }else{

                //$scope.userList.groupId = $scope.groupId;
                //$scope.userList.groupName = $scope.groupName;

                $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/updateNumberGroup/'+ $scope.groupId+'/' + $scope.groupName, $scope.userList, {
                    headers: {'Content-Type': 'application/json;charset=utf-8'}
                }).success(function (results) {
                    $scope.userList = null;
                    $('#usernumber_model').modal('hide');
                    $scope.getNumberGroupList();
                }).error(function () {
                    alert('保存号码组失败!');
                });
            }

        }

        $scope.deleteGroup = function(group){

            $http.delete('http://' + backOfficeHost + '/rest/v1.0/backoffice/user/deleteNumberGroup/'+ group.objId , {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (results) {
                //console.debug(results.data);
                $scope.getNumberGroupList();
            }).error(function () {
                alert('删除号码组错误!');
            });

        }

    }]);